
package server.cmd;

import java.sql.Connection;
import java.util.ArrayList;

import server.dao.TbCardDAO;
import server.vo.TbCardVO;

public class TbCardCmd {

	public String inqrCardBillList(Connection conn, 
			String bill_code,
			String user_id,
			String state, 
			String type) throws Exception {

		TbCardVO tbCardVO = new TbCardVO();
		TbCardDAO tbCardDAO = new TbCardDAO();

		if("%".equals(bill_code)) {
			bill_code = "";
		}
		if("A".equals(state)) {
			state = "";
		}
		if("A".equals(type)) {
			type = "";
		}

		tbCardVO.setUser_id(user_id.trim());
		tbCardVO.setBill_code(bill_code.trim());
		tbCardVO.setState(state.trim());
		tbCardVO.setType(type.trim());

		String szMsg ="";

		try {

			ArrayList<TbCardVO> al = tbCardDAO.inqrCardBillList(conn, tbCardVO);

			for (int i=0; i<al.size(); i++)
			{
				TbCardVO result = al.get(i);
				szMsg += result.getBill_code()+"|";
				szMsg += result.getUser_id()+"|";
				szMsg += result.getBill_date()+"|";
				szMsg += result.getBill_amount()+"|";
				szMsg += result.getShop_name()+"|";
				szMsg += result.getShop_location()+"|";
				szMsg += result.getState()+"|";
				szMsg += result.getProcess_date()+"|";
				szMsg += result.getType()+"^";
			}		
			return szMsg;	

		} catch(Exception e) {
			throw e;
		}	
	}

	public String insertCardBill(Connection conn, 
			String bill_date, 
			String user_id,
			String bill_amount, 
			String shop_name, 
			String shop_location,
			String state,
			String process_date,
			String type
			
			) throws Exception {


		TbCardVO tbCardVO = new TbCardVO();		
		TbCardDAO tbCardDAO = new TbCardDAO();


		String szMsg ="SUCCESS";

		//201209091200  
		//0123456789011
		if(bill_date.length() != 12) {
			throw new Exception("bill_date YYYYMMDDHH24MI");
		}

		String formatDate_billdate = "";
		formatDate_billdate += bill_date.substring(0, 4);
		formatDate_billdate += "-";
		formatDate_billdate += bill_date.substring(4, 6);
		formatDate_billdate += "-";
		formatDate_billdate += bill_date.substring(6, 8);
		formatDate_billdate += " ";
		formatDate_billdate += bill_date.substring(8, 10);
		formatDate_billdate += ":";
		formatDate_billdate += bill_date.substring(10, 12);



		tbCardVO.setUser_id(user_id);
		tbCardVO.setBill_date(formatDate_billdate);
		tbCardVO.setBill_amount(Long.parseLong(bill_amount));
		tbCardVO.setShop_name(shop_name);
		tbCardVO.setShop_location(shop_location);
		tbCardVO.setState(state);

		String formatDate_processdate = "";
		if(process_date.length() == 12) {
			formatDate_processdate += process_date.substring(0, 4);
			formatDate_processdate += "-";
			formatDate_processdate += process_date.substring(4, 6);
			formatDate_processdate += "-";
			formatDate_processdate += process_date.substring(6, 8);
			formatDate_processdate += " ";
			formatDate_processdate += process_date.substring(8, 10);
			formatDate_processdate += ":";
			formatDate_processdate += process_date.substring(10, 12);
		} else if(process_date.isEmpty()){
			formatDate_processdate = null;
		} else {
			throw new Exception("wrong process_date");
		}

		tbCardVO.setProcess_date(formatDate_processdate);
		tbCardVO.setType(type);

		try {

			// get bill code
			String bill_code = tbCardDAO.inqrMaxCardBillCode(conn);
			tbCardVO.setBill_code(bill_code);

			//insert
			boolean results = tbCardDAO.insertCardBill(conn, tbCardVO);

			if(!results){
				throw new Exception("fail");
			}


			return szMsg;	

		} catch(Exception e) {
			throw e;
		}	
	}


	public String updateCardBill(Connection conn, 
			String bill_code, 
			String state, 
			String process_date	) throws Exception {

		TbCardVO tbCardVO = new TbCardVO();		
		TbCardDAO tbCardDAO = new TbCardDAO();
		String szMsg ="SUCCESS";

		tbCardVO.setBill_code(bill_code);
		tbCardVO.setState(state);
		tbCardVO.setProcess_date(process_date);


		try {


			boolean results = tbCardDAO.updateCardBill(conn, tbCardVO);	
			if(!results){
				throw new Exception("fail");
			}

			return szMsg;	

		} catch(Exception e) {
			throw e;
		}	
	}







}
